from google.appengine.ext import webapp
from DataClass.FileData import FileData
from google.appengine.api import users
from google.appengine.ext.webapp import template
from AdminBase import AdminBase
from DataClass.WebsiteRec import WebsiteRec
from google.appengine.ext import db

import os
import cgi

class AdminDetailBase(AdminBase):
    def GetItemTemplateFileName(self):
        raise Exception("GetItemTemplateFileName not implemented")
    
    def GetItem(self, key):
        raise Exception("GetItem not implemented")
    
    def GetFormAction(self):
        raise Exception("GetFormAction not implemented")

    def GetDetailHeading(self):
        raise Exception("GetDetailHeading not implemented")

    def PostIsAddNew(self, user):
        raise Exception("PostIsNew not implemented")

    def ValidateAddNewPost(self, user):
        raise Exception("ValidateNewPost not implemented")
        
    def ValidateUpdatePost(self, user):
        raise Exception("ValidateUpdatePost not implemented")
    
    def PerformAddNewFromPost(self, user):
        raise Exception("PerformAddNewPost not implemented")

    def PerformUpdateFromPost(self, user):
        raise Exception("PerformUpdatePost not implemented")
    
#######################################

    def IsDelete(self):
        querystring=cgi.FieldStorage()
        dodelete = (querystring.getvalue("delete", "False") == "True")
        return dodelete

            



    
    def GetTemplateFileName(self):
        if self.IsDelete():
            retval = 'PSPages/admindetail-delete.html'
        else:
            retval = 'PSPages/admindetail.html'
        return retval;
        
    def GetBreadcrumbList(self):
        Website = WebsiteRec.GetCurrentWebsite(self.request)
        return [['main', 'admin'],['files', 'editfiles'],['editfile']]
    
    def RenderItem(self, aItem, template_values):
        path = os.path.join(os.path.dirname(__file__), self.GetItemTemplateFileName())
        item_template_values = {}
        item_template_values['item'] = aItem
        item_template_values.update(template_values)
        renderedhtml = template.render(path, item_template_values)
        return renderedhtml

    def GetTemplateValuesDict(self, template_values, user):
        retval = template_values

        item = None
        
        querystring=cgi.FieldStorage()
        key =  querystring.getvalue("key", None)
        if key:
            item = self.GetItem(key)
            
        lsuccessmessage =  querystring.getvalue("msg", None)
        lerrormessage =  querystring.getvalue("error", None)
            
        
#        if not item:
#            item = self.CreateEmptyItem()
        if item:
            actionforheading = "Update"
        else:
            actionforheading = "New"
        
        itemhtml = self.RenderItem(item, template_values)
        
        #construct full formaction from method and current querystring
        formaction = self.GetFormAction()
#        for lkey in querystring.keys():
#            lvalue = querystring.getvalue(lkey)
#            if "?" in lkey:
#                formaction += "&%s=%s" % (lkey, lvalue)
#            else:
#                formaction += "?%s=%s" % (lkey, lvalue)
        
        template_values['successmessage'] = lsuccessmessage
        template_values['errormessage'] = lerrormessage
        template_values['itemhtml'] = itemhtml
        template_values['formaction'] = formaction
        template_values['detailheading'] = self.GetDetailHeading()
        template_values['actionforheading'] = actionforheading
                      
        return retval

    def DoPost(self, user):
        if self.IsDelete():
            pass
        else:
            SuccessMessage = None
            ErrorMessage = None
            
            # this is the update post
            if self.PostIsAddNew(user):
                if self.ValidateAddNewPost(user):
                    lkey = self.PerformAddNewFromPost(user)
                    SuccessMessage = "New item added successfully"
                else:
                    ErrorMessage = "Validation Error"
            else:
                if self.ValidateUpdatePost(user):
                    lkey = self.PerformUpdateFromPost(user)
                    SuccessMessage = "Update performed successfully"
                else:
                    ErrorMessage = "Validation Error"
            
            fullformaction = "%s?key=%s" % (self.GetFormAction(), lkey)
            
            if SuccessMessage:
                fullformaction = "%s&msg=%s" % (fullformaction, SuccessMessage)
            
            if ErrorMessage:
                fullformaction = "%s&error=%s" % (fullformaction, ErrorMessage)
            
            self.redirect(fullformaction)
